<?php

class ThemeController extends MyController
{	
	function init() {parent::init(); }
	
	function actionIndex()
	{	
		$this->redirect(array($this->controller_id.'/view'));
	}
	
	function actionView($page = 1)
	{
		$pages = new CPagination;
		$pages->pageSize = $this->website["beperpage"];

		$rows = NULL;

		$rows = theme::model()->findAll(array(
			"order" => "`name` ASC",
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		// tong so dong
		$pages->itemCount =  theme::model()->count();
			
		$this->data['rows']= $rows;		
		
		$this->data['pages']= $pages;	
		$this->data['page']= $page;	
		$this->render('view', $this->data);
	}
	
	function actionAdd($page = 1)
	{	
		$this->action = "Thêm mới";
		$row = new theme;
		$row->id = 0;
		
		$this->data["row"] = $row;
		$this->data["page"] = $page;
		$this->render("form_edit", $this->data);
	}
	
	function actionEdit($theme_id = 0, $page = 1)
	{
		$row = NULL;
		if ($theme_id == 0)
		{
			$this->redirect(array($this->controller_id.'/view', "page"=>$page));
		}else{	
			$row = theme::model()->findByPk($theme_id);
		}
		
		$this->data["row"] = $row;
		$this->data["page"] = $page;
		
		$this->action = "Cập nhật";
		$this->render("form_edit", $this->data);
	}
	
	function actionSave($theme_id = 0, $page = 0)
	{
		if ($theme_id == 0) // new theme
		{	
			$row = new theme;
			$row->setAttributes($_POST, false);
			if (strlen($row->name))
				$row->save();
		}
		else
		{
			$row = theme::model()->findByPk($theme_id);
			$row->setAttributes($_POST, false);
			if(strlen($row->name))
				$row->save();
		}
		$this->redirect(array($this->controller_id."/view", "page"=>$page));
	}
	
	function actionDelete($theme_id, $page= 1)
	{
		theme::model()->deleteByPk($theme_id);
		
		$rows = theme::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if($page > 1)
				$page--;
		}
		
		$this->redirect(array($this->controller_id.'/view',"page"=>$page));
	}
	
	function actionDefault($theme_id = 0, $page = 1)
	{
		theme::model()->updateAll(array("default"=>0));
		$row = theme::model()->findByPk($theme_id);
		$row->default = 1;
		$row->save();	
		$this->redirect(array($this->controller_id."/view", "page"=>$page ));
	}
	
	function actionDelete_multi($page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		
		if (!is_null($id_array))
		{			
			foreach($id_array as $id=>$value)
			{
				theme::model()->deleteByPk($id);
			}
		}
		
		$rows = theme::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if($page > 1)
				$page--;
		}
		
		$this->redirect(array($this->controller_id.'/view',  "page"=>$page));
	}

}